import { defineStore } from 'pinia'
import { reqCategory1List, reqCategory2List, reqCategory3List, reqAttrList } from '@/api/product/attr'
import type { CategoryState } from './types/type'
import { ElMessage } from 'element-plus'

export const useCategoryStore = defineStore('Category', {
  state: (): CategoryState => {
    return {
      c1Arr: [],
      c2Arr: [],
      c3Arr: [],
      attrList: [],
      c1Id: '',
      c2Id: '',
      c3Id: '',
    }
  },
  actions: {
    async reqCategory1List() {
      const result = await reqCategory1List()
      if (result.code === 200) {
        this.c1Arr = result.data
        return
      } else {
        ElMessage.error({
          message: '请求一级分类失败',
        })
        return
      }
    },

    async reqCategory2List(category1Id: number|string) {
      const result = await reqCategory2List(category1Id)
      if (result.code === 200) {
        this.c2Arr = result.data
        return
      } else {
        ElMessage.error({
          message: '请求一级分类失败',
        })
        return
      }
    },
    async reqCategory1ListCategory3List(category2Id: number | string) {
      const result = await reqCategory3List(category2Id)
      if (result.code === 200) {
        this.c3Arr = result.data
        return 
      } else {
        ElMessage.error({
          message: '请求一级分类失败',
        })
        return
      }
    },
    // 获取属性列表
    async reqAttrList() {
      const result = await reqAttrList(this.c3Id)
      if (result.code === 200) {
        this.attrList = result.data
        return
      } else {
        ElMessage.error({
          message: '请求属性列表失败',
        })
        return
      }
    },
  },
})
